home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1191 / 1191.xpi / chrome / reminderfox.jar / content / reminderfox / utils / rmFxAttachments.js next >
Text File  |  2009-11-01  |  8KB  |  225 lines

  1. /**
  2.  *         Eventhandler working for messages with ICS attachment
  3. <br>         replacing the attachment name with the METHOD string contained
  4. <br>        in the Vcalendar
  5.  
  6. @see     +++2do: 2009-06-20  current version is NOK with inline vcalendar 
  7.  
  8.         @author gW
  9.         @version    2009-06-20
  10.  
  11.  */    
  12.  
  13. var rmFx_icsString = "";
  14. var rmFxEventIcon = "chrome://reminderfox/skin/images/eventIcon.png";
  15. var rmFxEventIconSize = 16;
  16.  
  17.  
  18.  
  19. var ICSgContentType;
  20.  
  21. var ICS_OnMsgLoaded_ORIGINAL = OnMsgLoaded;
  22.  
  23. var OnMsgLoaded = function(aUrl) {
  24.     ICS_OnMsgLoaded_ORIGINAL.apply(this,arguments);
  25.     
  26.     // "ltnModeBox"  part of Lightning, disabled here to have feature also w.LG
  27.     // if (document.getElementById("ltnModeBox"))     return;
  28.  
  29.     var mimeHdr = aUrl.mimeHeaders;
  30.     
  31.     ICSgContentType = mimeHdr.extractHeader("Content-type",false);
  32.  
  33.   //rmFxUtil.dump2Console (" ICSattachment  OnMsgLoaded:" + ICSgContentType);
  34.  
  35.     // Vcalendar requests seem to be present just in multipart/alternative body
  36.     if (ICSgContentType.indexOf("text/plain") > -1 || ICSgContentType.indexOf("text/html") > -1) 
  37.         return;
  38.         
  39.     // The primary content-type is useful, to find the boundary string, just if the message
  40.     // is multipart/alternative, otherwise we should find the right boundary string in another way
  41.     if (ICSgContentType.indexOf("multipart/alternative") < 0 ) 
  42.         ICSgContentType = null;
  43.  
  44.     // Postbox needs this timeout, so it can load all ICS attachments to the datapane Events
  45.     setTimeout(ICSparseMsg.start,100);    
  46.  
  47.     
  48. };
  49.  
  50.  
  51. var ICSparseMsg = {
  52.     newAttListner: function() {
  53.         var attListner = {
  54.  
  55.             scriptStream : null,
  56.             
  57.             QueryInterface : function(iid)  {
  58.                 if (iid.equals(Components.interfaces.nsIStreamListener) ||   
  59.                     iid.equals(Components.interfaces.nsIMsgHeaderSink) ||
  60.                     iid.equals(Components.interfaces.nsISupports))
  61.                  return this;
  62.         
  63.                 throw Components.results.NS_NOINTERFACE;
  64.                 return 0;
  65.             },
  66.         
  67.             onStartRequest : function (aRequest, aContext) {
  68.                     var channel = aRequest.QueryInterface(Components.interfaces.nsIChannel);
  69.                     channel.URI.QueryInterface(Components.interfaces.nsIMsgMailNewsUrl);
  70.                     channel.URI.msgHeaderSink = this; 
  71.  
  72.                     this.scriptStream = Components.classes['@mozilla.org/binaryinputstream;1']
  73.                             .createInstance(Components.interfaces.nsIBinaryInputStream);
  74.             },
  75.                    
  76.             onStopRequest : function (aRequest, aContext, aStatusCode) {
  77.                 this.scriptStream = null;
  78.             },
  79.  
  80.             
  81.             onDataAvailable : function (aRequest, aContext, aInputStream, aOffset, aCount) {
  82.                 
  83.                 this.scriptStream.setInputStream(aInputStream);
  84.                 rmFx_icsString = this.scriptStream.readBytes(aCount);                
  85.      //   rmFxUtil.dump2Console("ICSparseMsg: onDataAvailable: \n\n" + rmFx_icsString);
  86.  
  87.                 var cMethod = "";
  88.  
  89.                  // delete all CR characters
  90.                  rmFx_icsString = rmFx_icsString.replace(new RegExp(/\r/g),"");  
  91.                     
  92.                 var fnd =  rmFx_icsString.indexOf("METHOD:");                        
  93.                 if (fnd != -1) {
  94.                      
  95.                      rmFx_icsString =  rmFx_icsString.substring(fnd + 7);
  96.                     cMethod = rmFx_icsString.substring( 0, rmFx_icsString.indexOf("\n"));
  97.  
  98.                     var theChild = document.getElementById("rmFX_Event");    
  99.                     
  100.                     if (theChild != null) {
  101.                     // replaced the file name
  102.                         theChild.attributes["label"].value = "iCal " + cMethod;
  103.                     // tag the msg    
  104.                         rmFx_msgTAGing("iCal-" + cMethod, true, "#993399");                
  105.             /* ------
  106.              *  +++2do doesn't work right now
  107.                      // enable dbl click to work with RmFx on the event
  108.                         theChild.setAttribute("ondblclick", "rmFxGetiCalMailed();");
  109.              ----- */
  110.                     }
  111.                 }
  112.             },
  113.             
  114.             onStartHeaders: function() {},
  115.     
  116.             onEndHeaders: function() {},
  117.             
  118.             processHeaders: function(headerNameEnumerator, headerValueEnumerator, dontCollectAddress) {},
  119.  
  120.             
  121.             
  122.             // The extension can work because natively Thunderbird
  123.             // collect in this functions also multipart/alternative parts
  124.             // even if they are not real attachments
  125.  
  126.             handleAttachment: function(contentType, url, displayName, uri, isExternalAttachment) {    
  127.     // rmFxUtil.dump2Console ("  handleAttachment: \n\n" + contentType + "    " + displayName);
  128.  
  129.    
  130.                    if ( displayName.indexOf(".vcs") < 0 
  131.                            && displayName.indexOf(".ics") 
  132.                            && (contentType == "text/calendar" || contentType =="text/x-calendar") ) {
  133.    
  134.                     var childList = document.getElementById('attachmentList');
  135.                         
  136.                     //  Postbox special
  137.                     if (childList == null)  
  138.                         childList = document.getElementById('dataPaneEventList');
  139.  
  140.                     var nodes = childList.childNodes.length
  141.                         
  142.                     for (var i=0; i< nodes; i++) {
  143.                         var cName = childList.childNodes[i].attachment.displayName;
  144.                         
  145.                         if (cName.toLowerCase().indexOf(".ics") > -1) {
  146.                             childList.childNodes[i].attributes["image"].value
  147.                                  = rmFxEventIcon + "?size=" + rmFxEventIconSize + "&contentType=text/calendar";
  148.  
  149.                             childList.childNodes[i].id ="rmFX_Event"
  150.                         }
  151.                     }
  152.                     this.index++;
  153.                 }
  154.             },
  155.             
  156.             onEndAllAttachments: function() {},      
  157.             onEndMsgDownload: function(url) {},      
  158.             onEndMsgHeaders: function(url) {},        
  159.             onMsgHasRemoteContent: function(aMsgHdr) {},       
  160.             getSecurityInfo: function() {},      
  161.             setSecurityInfo: function(aSecurityInfo) {},     
  162.             getDummyMsgHeader: function() {}
  163.         };
  164.         return attListner;
  165.         
  166.     },
  167.      
  168.     start: function() {
  169.         
  170.         rmFx_icsString = "";
  171.         
  172.         // SM2  ???   || (gRmFx_Messenger == "SM2") //   || (gRmFx_Messenger == "PB")) 
  173.         
  174.     /*    
  175.         if (gRmFx_Messenger == "TB3")  {  
  176.             var msgUri =  gMessageDisplay.folderDisplay.selectedMessageUris[0];
  177.         }  else {
  178.             var msgUri = GetLoadedMessage();
  179.         }
  180.     */    
  181.         if ((gRmFx_Messenger == "TB3") || (gRmFx_Messenger == "SM2")) {
  182.             var msgUri =  gFolderDisplay.selectedMessageUris[0];
  183.         }
  184.         var mms = messenger.messageServiceFromURI(msgUri)
  185.             .QueryInterface(Components.interfaces.nsIMsgMessageService);
  186.         var streamListner = ICSparseMsg.newAttListner(); 
  187.         streamListner.index = 0;  
  188.         var newUri = mms.streamMessage(msgUri, streamListner, null, null, true, "filter");
  189.         
  190.         var x=0;
  191.     }
  192. }
  193. /**
  194.  *     Read the whole selected message (including attachments) to a string
  195.  * 
  196. <p>     @return    content    , if read fails: alert and return null
  197.  */
  198. function rmFxReadSelectedMsg () {
  199.     var content = "";
  200.       var MessageURI = GetFirstSelectedMessage();
  201.       var MsgService = messenger.messageServiceFromURI(MessageURI);
  202.       
  203.       var MsgStream =  Components.classes["@mozilla.org/network/sync-stream-listener;1"]
  204.                                 .createInstance();
  205.       var consumer = MsgStream.QueryInterface(Components.interfaces.nsIInputStream);
  206.       
  207.       var ScriptInput = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance();
  208.       var ScriptInputStream = ScriptInput.QueryInterface(Components.interfaces.nsIScriptableInputStream);
  209.       
  210.       ScriptInputStream.init(consumer);
  211.       
  212.       try {
  213.         MsgService.streamMessage(MessageURI, MsgStream, msgWindow, null, false, null);
  214.       } catch (ex) {
  215.         alert(" Error: "+ex);
  216.         return null;
  217.       }
  218.       ScriptInputStream .available();
  219.       while (ScriptInputStream .available()) {
  220.         content = content + ScriptInputStream .read(512);
  221.       }
  222.       
  223.       return content;
  224. }
  225.